<?php
	include("connect.php");
	$css = '<link rel="stylesheet" type="text/css" media="all" href="asset/css/class_home.css">';
	$title = 'homepage';
	$page_name = 'index';

//check if the user is logging out, then erase the session
/*if(isset($_REQUEST["logout"]))
{
	if($_REQUEST["logout"]==1)
		unset($_SESSION["userid"]);
}*/

$sql = "select user_category from m_user where user_id=".$_SESSION['userid'];
$retval = mysql_query( $sql, $con );
while($row = mysql_fetch_array($retval))
	{
		$user_category = $row[0];
	}
if(isset($_SESSION["userid"]))
	{
		if($user_category ==9)
		{
			include("header_loggedinadmin.php");
		}else
		{
			include("header_loggedin.php");
		}
	}
else
{
	include("header.php");
}
	
?>
<script type="text/javascript" src="asset/js/jquery-1.4.2.min.js"></script>
<div id="blue_bg">
	<div id="main">
    	<div id="header">
        	<div id="header_box">
            	<div id="search_book_btn"><a href=""><img src="images/home/search-book-btn.png" /></a></div>
                <div id="sell_book_btn"><a href="sell_book.php"><img src="images/home/sell-book-btn.png" /></a></div>
            </div>
        </div>
        <div class="wrapper">
        	<div id="content">
            	<div class="box_340px">
                	<h3 class="flag_short">REQUESTED BOOKS</h3>
                    <?php
					$sql = "SELECT a.request_book_id, b.fname, c.book_title, c.book_author ";
					$sql .= "FROM t_request_book a, m_user b , m_book c ";
					$sql .= "WHERE a.user_id = b.user_id AND a.book_id = c.book_id ";
					$sql .= "ORDER BY a.request_book_id DESC ";
					$sql .= "LIMIT 0 , 2 ";
					$retval = mysql_query($sql);
					if (!$retval)
					{
						die('Could not execute query: ' . mysql_error());
					}
					while($row = mysql_fetch_array($retval))
					{
						echo '<div class="book_box">';
                    	echo '<div class="small_book_thumb">';
							echo '<a href="book_detail.php?userbookid=' .$row['request_book_id'] .'&type=requested' .'">';
                        	echo '<img src="images/home/book-thumb.jpg" />';
							echo '</a>';
                        echo '</div>';
                        echo '<div class="book_column_thumb">';
                        	echo '<a href="book_detail.php?userbookid=' .$row['request_book_id'] .'&type=requested' .'">' .$row['book_title'] .'</a><br />';
                            echo 'Writer <span class="bold">' .$row['book_author'] .'</span><br />';
							echo 'Request by <span class="italic"><a href=""> ' .$row['fname'] .'</a></span>';
                        echo '</div>';
                        echo '<div class="clear"></div>';
                    	echo '</div>';
					}
					?>
                    <div class="more_btn"><a href="requested_book.php">MORE BOOK</a></div>
                </div>
                <div class="box_340px">
                	<h3 class="flag_short">BOOKS ON SELL</h3>
                    <?php
					$sql = "SELECT a.user_book_id, a.price, b.book_title, b.book_author, c.fname, d.bookphoto_link ";
					$sql .= "FROM t_user_book a, m_book b, m_user c, t_book_photo d ";
					$sql .= "WHERE a.book_id = b.book_id AND a.user_id = c.user_id ";
					$sql .= "AND a.user_book_id = d.user_book_id AND bookphoto_series = 1 ";
					$sql .= "ORDER BY a.user_book_id DESC ";
					$sql .= "LIMIT 0,2 ";
					$retval = mysql_query($sql);
					if (!$retval)
					{
						die('Could not execute query: ' . mysql_error());
					}
					while($row = mysql_fetch_array($retval))
					{
						echo '<div class="book_box">';
                    	echo '<div class="small_book_thumb">';
							echo '<a href="book_detail.php?userbookid=' .$row['user_book_id'] .'&type=sale' .'">';
							echo '<img src="images/upload/' .$row['bookphoto_link'] .'" width="44" height="56" />';
							//echo '<img src="images/home/book-thumb.jpg" />';
							echo '</a>';
                        echo '</div>';
                        echo '<div class="book_column_thumb">';
                        	echo '<a href="book_detail.php?userbookid=' .$row['user_book_id'] .'&type=sale' .'">' .$row['book_title'] .'</a> (' .$row['price'] .' SGD)<br />';
                            echo 'Writer <span class="bold">' .$row['book_author'] .'</span><br />';
							echo 'Upload by <span class="italic"><a href="">' .$row['fname'] .'</a></span>';
                        echo '</div>';
                        echo '<div class="clear"></div>';
                    echo '</div>';
					}
                    ?>
                    <div class="more_btn"><a href="book_to_sell.php">MORE BOOK</a></div>
                </div>
                <div class="box_340px_nopadding">
                	<h3 class="flag_short">POPULAR BOOK</h3>
                    <?php
                    $sql = "SELECT b.book_title, c.bookphoto_link ";
					$sql .= "FROM t_user_book a, m_book b, t_book_photo c ";
					$sql .= "WHERE a.book_id = b.book_id AND a.user_book_id = c.user_book_id ";
					$sql .= "GROUP BY a.book_id ";
					$sql .= "ORDER BY COUNT(a.book_id) DESC ";
					$sql .= "LIMIT 0 , 1";
					$retval = mysql_query($sql);
					if (!$retval)
					{
						die('Could not execute query: ' . mysql_error());
					}
					while($row = mysql_fetch_array($retval))
					{
						echo '<div class="book_box">';
                    	echo '<div class="book_thumb">';
						echo '<a href="book_to_sell.php?bookid=">';
                        	echo '<img src="images/upload/' .$row['bookphoto_link'] . '" width="109" height="125" />';
						echo '</a>';
                        echo '</div>';
                        echo '<div class="book_column">';
                        	echo '<a href="book_to_sell.php?bookid=">' .$row['book_title'] .'</a><br />';
                            echo 'Writer <span class="bold">Robert Wiliam</span><br />';
							echo 'Much has changed since the first edition of Human-Computer Interaction was published. Ubiquitous computing and rich sensor-filled environments are developed nowadays.';
                        echo '</div>';
                        echo '<div class="clear"></div>';
                    echo '</div>';
					}
					?>
                    <div class="more_btn"><a href="book_on_sell.php">MORE BOOK</a></div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
    </div>
</div>


<?php
 include("footer.php");
?>